package com.habdqn.studentmanager.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.habdqn.studentmanager.bean.Classes;
import com.habdqn.studentmanager.bean.Teacher;
import com.habdqn.studentmanager.constant.ClassType;
import com.habdqn.studentmanager.constant.Constant;
import com.habdqn.studentmanager.constant.LogType;
import com.habdqn.studentmanager.exception.StudentException;
import com.habdqn.studentmanager.mapper.ClassesMapper;
import com.habdqn.studentmanager.mapper.TeacherMapper;
import com.habdqn.studentmanager.service.TeacherService;
import com.habdqn.studentmanager.util.MD5Util;
import com.habdqn.studentmanager.util.ProjectUtil;
import com.habdqn.studentmanager.util.SystemLogger;

/**
 * @author Administrator 教师服务类 2017年5月17日
 * 
 */
@Service
public class TeacherServiceImpl implements TeacherService {

	private static Logger logger = Logger.getLogger(TeacherServiceImpl.class);

	@Resource
	private TeacherMapper teacherMapper;

	@Resource
	private SystemLogger systemLogger;
	
	@Resource
	private ClassesMapper classMapper;

	@Override
	@Transactional
	public void add(Teacher bean) {

		try {
			// MD5(账号+密码明文)加密保存
			String password = bean.getAccount() + Constant.DEFAULT_PASSWORD;
			password = MD5Util.getMD5(password);
			bean.setPassword(password);
			teacherMapper.insert(bean);
			systemLogger.record(LogType.AddType, "新增教师:" + bean.getAccount());
		}
		catch (DuplicateKeyException e) {
			logger.debug("duplicateKey: " + bean.getAccount(), e);
			throw new StudentException("账号重复, 请重新输入", e);
		}
		catch (NoSuchAlgorithmException e) {
			logger.error("加密失败", e);
			throw new StudentException("加密失败", e);
		}
		catch (UnsupportedEncodingException e) {
			logger.error("错误的编码", e);
			throw new StudentException("错误的编码", e);
		}
		catch (RuntimeException e) {
			logger.error("system error", e);
			throw new StudentException("系统异常", e);
		}
	}

	@Override
	public void update(Teacher bean) {
		try {
			teacherMapper.update(bean);
			systemLogger.record(LogType.UpdateType, "编辑教师:" + bean.getId());
		}
		catch (RuntimeException e) {
			logger.error("更新教师失败", e);
			throw new StudentException("更新教师失败", e);
		}

	}

	@Override
	public void delete(Teacher bean) {
		
		Integer status = ClassType.open.toInteger();
		
		bean = teacherMapper.find(bean);
		
		if (null == bean) {
			throw new StudentException("该教师不存在");
		}
		
		int count = classMapper.findByTeacher(bean, status);
		if (count > 0) {
			throw new StudentException("该教师还关联着未结课的班级");
		}
		
		Teacher current = ProjectUtil.getCurrentUser();
		if (current.getId().equals(bean.getId())) {
			throw new StudentException("不能删除自己");
		}
		
		try {
			teacherMapper.delete(bean);
			systemLogger.record(LogType.DelType, "删除教师:" + bean.getId());
		}
		catch (RuntimeException e) {
			logger.error("system error", e);
			throw new StudentException("系统异常, 删除失败", e);
		}

	}

	@Override
	public Teacher find(Teacher bean) {
		
		try {
			return teacherMapper.find(bean);
		}
		catch (RuntimeException e) {
			logger.debug("system error", e);
			throw new StudentException("查询教师失败", e);
		}
	}

	@Override
	public PageInfo<Teacher> findList(Teacher bean, Integer pageNum, Integer pageSize) {
		
		try {
			PageHelper.startPage(pageNum, pageSize);
			return new PageInfo<Teacher>(teacherMapper.findList(bean));
		}
		catch (RuntimeException e) {
			logger.error("system error", e);
			throw new StudentException("查询教师列表失败", e);
		}
	}

	@Override
	public List<Teacher> findListByClass(Classes bean, Integer type) {
		
		try {
			return teacherMapper.findListByClass(bean, type);			
		}
		catch (RuntimeException e) {
			logger.error("system error", e);
			throw new StudentException("查询教师列表失败", e);
		}
	}

}
